<template>
	<view class="container">
		<view>
			<!-- <view style="display: flex;justify-content: space-between;line-height: 100rpx;">
				<view style="width:375rpx;height: 100rpx;text-align:center;font-size: 36rpx;font-weight: bold;background-color: #00aaff;color: #fff;border-radius: 10rpx;">
					增加巡查明细
				</view>
				<view @click="removeDetail(index)" style="width: 50rpx;height: 50rpx;background-color: red;color: #fff;display: flex;justify-content: center;text-align: center;">x</view> 
			</view> -->
			<view class="input-row" v-if="ptype=='03'">
				<view style="width: 200rpx;">质量检查项目：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindChangesqtype" range-key="cn"
					:data-index="22" :data-id="qtypes[qtypesindex].id" :value="index" :range="qtypes">
					<view class="uni-input">{{qtypes[qtypesindex].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="qtypes[qtypesindex].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">项目大类：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindChangespiid1" range-key="cn"
					:data-index="22" :data-id="piid1s[piid1index].id" :value="index" :range="piid1s">
					<view class="uni-input">{{piid1s[piid1index].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="piid1s[piid1index].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">项目中类：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindChangespiid2" range-key="cn"
					:data-index="22" :data-id="piid2s[piid2index].id" :value="index" :range="piid2s">
					<view class="uni-input">{{piid2s[piid2index].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="piid2s[piid2index].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">项目小类：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindChangespiid3" range-key="cn"
					:data-index="22" :data-id="piid3s[piid3index].id" :value="index" :range="piid3s">
					<view class="uni-input">{{piid3s[piid3index].cn}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="piid3s[piid3index].id" hidden />
				</picker>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">巡查照片一：</view>
				<image :src="img1" v-if="img1!=''" @click="imgPreview(img1)" style="width: 200rpx;height: 200rpx;">
				</image>
				<view @click="chooseImg1" v-if="img1==''" style="height: 200rpx;width: 200rpx;">
					<image src="/static/images/add1.png" style="width: 200rpx;height: 200rpx;"></image>
				</view>
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">巡查照片二：</view>
				<image :src="img2" v-if="img2!=''" @click="imgPreview(img2)" style="width: 200rpx;height: 200rpx;">
				</image>
				<view @click="chooseImg2" v-if="img2==''" style="height: 200rpx;width: 200rpx;">
					<image src="/static/images/add1.png" style="width: 200rpx;height: 200rpx;"></image>
				</view>
			</view>
			<view class="input-row" v-if="ptype=='02'">
				<view style="width: 200rpx;">隐患范围：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindChangedangerange" range-key="cn"
					:data-index="22" :data-id="dangeranges[dangerangeindex].id" :value="dangerangeindex"
					:range="dangeranges">
					<view class="uni-input">{{dangeranges[dangerangeindex].cn}}</view>

					<input type="text" :value="dangeranges[dangerangeindex].id" hidden />
				</picker>
			</view>
			<view class="input-row" v-if="ptype=='02'">
				<view style="width: 200rpx;">隐患类别：</view>
				<picker style="width: 500rpx;text-align: center;" @change="bindChangesdangetype" range-key="cn"
					:data-index="22" :data-id="dangetypes[dangetypeindex].id" :value="index" :range="dangetypes">
					<view class="uni-input">{{dangetypes[dangetypeindex].cn}}</view>

					<input type="text" :value="dangetypes[dangetypeindex].id" hidden />
				</picker>
			</view>
			<view class="input-row" v-if="ptype=='02'||ptype=='03'">
				<view style="width: 200rpx;">隐患数量：</view>
				<input style="width: 500rpx;" type="number" v-model="dangecount" />
			</view>
			<view class="input-row" v-if="ptype=='02'||ptype=='03'">
				<view style="width: 200rpx;">隐患描述：</view>
				<textarea style="width: 500rpx;" height="100rpx" v-model="dangedetail" />
			</view>
			<view class="input-row">
				<view style="width: 200rpx;">得分：</view>
				<textarea style="width: 500rpx;" height="100rpx" v-model="ipdscore" />
			</view>
		</view>
		<view style="display: flex;align-items: center;text-align: center;margin-bottom: 30rpx;">
			<button @click="adddetail"
				style="width: 500rpx;height: 80rpx;background-color: #00aaff;color: #fff;">保存退出</button>
		</view>
		<!-- <view style="display: flex;align-items: center;text-align: center;margin-bottom: 30rpx;">
			<button @click="adddetail"
				style="width: 500rpx;height: 80rpx;background-color: #ffbb1d;color: #fff;">增加巡查明细</button>
		</view> -->
	</view>
</template>

<script>
	import {
		request
	} from '@/request/index.js';
	export default {
		data() {
			return {
				img1: '',
				img2: '',
				pphoto1: '',
				pphoto2: '',
				pid: '',
				userInfo: {},

				dangecount: 1,
				dangedetail: '',
				piid1s: [],
				piid1index: 0,
				piid1: '',
				piid2s: [],
				piid2index: 0,
				piid2: '',
				piid3s: [],
				piid3index: 0,
				piid3: '',
				dangeranges: [],
				dangerange: '',
				dangerangeindex: 0,
				dangetypes: [],
				dangetype: '',
				dangetypeindex: 0,
				ipdscore: '',
				ptype: '',
				qtypesindex:0,
				qtypes:[],
				qtype:''
			};
		},
		onLoad(option) {

			this.pid = option.pid
			this.ptype = option.ptype
			var userInfo = uni.getStorageSync('user');
			// if (userInfo == null || userInfo == 'null' || userInfo == '') {
			// 	uni.showToast({
			// 		title: "请先登录",
			// 		success() {
			// 			uni.reLaunch({
			// 				url: "/pages/login/login"
			// 			})
			// 		}
			// 	})

			// }

			this.getPiid1s()
			this.getPiid2s()
			this.getPiid3s()
			this.getqtype()
			this.getDangeranges()
			this.getDangetypes()
			var _self = this;
			_self.userInfo = userInfo
		},

		methods: {
			imgPreview(url) {
				uni.previewImage({
					urls: [url]
				})
			},
			getScore() {
				var that = this
				request({
					url: "/patrol2/ComputePatrolScore?pids=" + that.pid,
					method: "GET",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {

					if (res.data.code == '200') {

					} else {
						uni.showModal({
							content: res.data.msg,
						})
					}
				})
			},
			bindChangesqtype(e){
				this.qtypesindex = e.target.value
				this.qtype = this.qtypes[this.qtypesindex].id
			},
			bindChangespiid1(e) {
				this.piid1index = e.target.value
				this.piid1 = this.piid1s[this.piid1index].id
			},
			bindChangespiid2(e) {
				this.piid2index = e.target.value
				this.piid2 = this.piid2s[this.piid2index].id
			},
			bindChangespiid3(e) {
				this.piid3index = e.target.value
				this.piid3 = this.piid3s[this.piid3index].id
			},
			bindChangedangerange(e) {
				this.dangerangeindex = e.target.value
				this.dangerange = this.dangeranges[this.dangerangeindex].id
			},
			bindChangesdangetype(e) {
				this.dangetypeindex = e.target.value
				this.dangetype = this.dangetypes[this.dangetypeindex].id
			},
			addsipdetail() {
				var data = {}
				var that = this
				var location = uni.getStorageSync("location")
				data.longitude = location.longitude;
				data.latitude = location.latitude;
				data.piid1 = that.piid1
				data.piid2 = that.piid2
				data.piid3 = that.piid3
				data.pphoto1 = that.pphoto1
				data.pphoto2 = that.pphoto2
				data.dangedetail = that.dangedetail
				data.dangerange = that.dangerange
				data.dangetype = that.dangetype
				data.dangecount = that.dangecount
				data.pid = that.pid
				var uri = ""
				if (this.ptype == '01') {
					uri = "/thirdForm/doAdd/c_cipdetail"
				}
				if (this.ptype == '02') {
					uri = "/thirdForm/doAdd/c_sipdetail"
				}
				if (this.ptype == '03') {
					uri = "/thirdForm/doAdd/c_qipdetail",
					data.qtype=that.qtype
				}
				request({
					url: uri,
					method: "POST",
					data: data
				}).then(resp => {
					if (resp.data.success) {
						this.getScore()
						uni.redirectTo({
							url: "/pages/cpatrol_detail/cpatrol_detail?pid=" + that.pid + "&ptype=" + this
								.ptype
						})
						// uni.showModal({
						// 	title:"提示",
						// 	content:"保存成功，是否继续添加巡查明细",
						// 	showCancel:true,
						// 	success:async(res) => {
						// 		if(res.confirm){
						// 			uni.redirectTo({
						// 				url:"/pages/cpatrol_detail/cpatrol_detail"
						// 			})
						// 		}else{
						// 			uni.redirectTo({
						// 				url:'/pages/cpatrol/cpatrol?ptype=01'
						// 			})
						// 		}
						// 	}
						// })
					} else {
						uni.showToast({
							title: resp.data.msg
						})
					}

				})
			},
			//增加明细
			adddetail() {
				this.addsipdetail()
			},
			chooseImg1() {
				var that = this
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;

						that.img1 = tempFilePaths[0]
						const uploadTask = uni.uploadFile({
							url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto1_file', // 开发者服务器的URL
							filePath: tempFilePaths[0],
							name: 'pphoto1_file', // 必填，文件对应的 key
							header: {
								'cookie': uni.getStorageSync('cookie')
							},
							formData: {
								'name': 'pphoto2_file' // 其他要上传的参数
							},
							success: (uploadFileRes) => {

								var data = JSON.parse(uploadFileRes.data)
								that.pphoto1 = data.data.fileName
							},
							fail: (uploadFileErr) => {
								console.error('upload fail:', uploadFileErr);
							}
						});
						// 监听上传进度变化
						uploadTask.onProgressUpdate((res) => {

						});
					},
					fail: (chooseImageErr) => {
						console.error('choose image fail:', chooseImageErr);
					}
				});
			},
			chooseImg2() {
				var that = this
				uni.chooseImage({
					count: 1, // 默认9，设置图片的数量
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;

						that.img2 = tempFilePaths[0]
						const uploadTask = uni.uploadFile({
							url: 'https://qscs.qtech2000.cn:8443/thirdUpload/img?name=pphoto2_file', // 开发者服务器的URL
							filePath: tempFilePaths[0],
							header: {
								'cookie': uni.getStorageSync('cookie')
							},
							name: 'pphoto2_file', // 必填，文件对应的 key
							success: (uploadFileRes) => {

								var data = JSON.parse(uploadFileRes.data)
								that.pphoto2 = data.data.fileName
							},
							fail: (uploadFileErr) => {
								console.error('upload fail:', uploadFileErr);
							}
						});
						// 监听上传进度变化
						uploadTask.onProgressUpdate((res) => {

						});
					},
					fail: (chooseImageErr) => {
						console.error('choose image fail:', chooseImageErr);
					}
				});
			},



			getPiid1s() {
				var that = this
				var uri = ""
				if (this.ptype == '01') {
					uri = "/thirdWidget/comboJson/c_cipdetail-piid1"
				}
				if (this.ptype == '02') {
					uri = "/thirdWidget/comboJson/c_sipdetail-piid1"
				}
				if (this.ptype == '03') {
					uri = "/thirdWidget/comboJson/c_qipdetail-piid1"
				}
				request({
					url: uri,
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {

					if (res.data.code == '200') {
						that.piid1s = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.piid1s.push(temp)
							}
						}
						that.piid1 = that.piid1s[that.piid1index].id
					} else {
						uni.showToast({
							title: res.data.msg,

						})
					}
				})
			},

			getPiid2s() {
				var that = this
				var uri = ""
				if (this.ptype == '01') {
					uri = "/thirdWidget/comboJson/c_cipdetail-piid2"
				}
				if (this.ptype == '02') {
					uri = "/thirdWidget/comboJson/c_sipdetail-piid2"
				}
				if (this.ptype == '03') {
					uri = "/thirdWidget/comboJson/c_qipdetail-piid2"
				}
				request({
					url: uri,
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {

					if (res.data.code == '200') {
						that.piid2s = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.piid2s.push(temp)
							}
						}
						that.piid2 = that.piid2s[that.piid2index].id
					} else {
						uni.showToast({
							title: res.data.msg,

						})
					}
				})
			},
			getPiid3s() {
				var that = this
				var uri = ""
				if (this.ptype == '01') {
					uri = "/thirdWidget/comboJson/c_cipdetail-piid3"
				}
				if (this.ptype == '02') {
					uri = "/thirdWidget/comboJson/c_sipdetail-piid3"
				}
				if (this.ptype == '03') {
					uri = "/thirdWidget/comboJson/c_qipdetail-piid3"
				}
				request({
					url: uri,
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {

					if (res.data.code == '200') {
						that.piid3s = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.piid3s.push(temp)
							}
						}
						that.piid3 = that.piid3s[that.piid3index].id
					} else {
						uni.showToast({
							title: res.data.msg,

						})
					}
				})
			},
			getqtype() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_qipdetail-qtype",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.code == '200') {
						that.qtypes = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.qtypes.push(temp)
							}
						}
						that.qtype = that.qtypes[that.qtypesindex].id
					} else {
						uni.showToast({
							title: res.data.msg,
						})
					}
				})
			},
			getDangeranges() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_ipdetail2-dangerange",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {

					if (res.data.code == '200') {
						that.dangeranges = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.dangeranges.push(temp)
							}
						}
						that.dangerange = that.dangeranges[that.dangerangeindex].id
					} else {
						uni.showToast({
							title: res.data.msg,

						})
					}
				})
			},

			getDangetypes() {
				var that = this
				//thirdWidget/comboJson/c_sipdetail-dangetype
				request({
					url: "/thirdWidget/comboJson/c_sipdetail-dangetype",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {

					if (res.data.code == '200') {
						that.dangetypes = []
						for (var i = 0; i < res.data.data.length; i++) {
							var temp = {}
							temp.id = res.data.data[i].id
							temp.cn = res.data.data[i].cn
							if (temp.id != '') {
								that.dangetypes.push(temp)
							}
						}
						that.dangetype = that.dangetypes[that.dangetypeindex].id
					} else {
						uni.showToast({
							title: res.data.msg,

						})
					}
				})
			},
		},

	};
</script>

<style>
	.container {
		padding: 20px;
	}

	.input-row {
		margin-bottom: 20px;
		display: flex;

	}

	.input-row text {
		flex-grow: 1;
		margin-right: 10px;
	}
</style>